#!/bin/bash
# Copyright (C) 2019 Checkmk GmbH - License: GNU General Public License v2
# This file is part of Checkmk (https://checkmk.com). It is subject to the terms and
# conditions defined in the file COPYING, which is part of this source code package.

# This file is for execution by the pre-commit framework.
#
set -e -o pipefail

declare -a fails

known_other_licened_files=(
    "cmk/notification_plugins/ilert.py"
    "cmk/notification_plugins/signl4.py"
)

is_known_exception() {
    local element
    local path="$1"
    for element in "${known_other_licened_files[@]}"; do [[ "${element}" == "$path" ]] && return 0; done
    return 1
}

for param in "$@"; do
    if [[ "$param" == tests* ]]; then
        continue
    fi
    if is_known_exception "${param}"; then
        continue
    fi
    if [[ "$param" == */cee/* || "$param" == */cme/* || "$param" == */cce/* || "$param" == */enterprise/* || "$param" == */managed/* || "$param" == */cloud/* || "$param" == */cse/* || "$param" == */saas/* || "$param" == non-free/* ]]; then
        if ! head -30 "$param" | grep -c -E "C[ME]E|Checkmk Enterprise License" >/dev/null; then
            fails+=("$param")
        fi
    else
        if ! head -25 "$param" | grep -c -E "GNU General Public" >/dev/null; then
            fails+=("$param")
        fi
    fi
done

log() {
    echo "$@" >&2
}

if [ ${#fails[*]} -ne 0 ]; then
    log "These files are missing licence headers:"
    log "${fails[@]}"
    log
    exit 1
fi
